package com.zzu.base.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zzu.base.common.enums.ResultStatusEnum;
import com.zzu.base.controller.role.vo.RolePageReqVO;
import com.zzu.base.entity.PermissionDO;
import com.zzu.base.entity.RoleDO;
import com.zzu.base.entity.RolePermissionDO;
import com.zzu.base.entity.UserRoleDO;
import com.zzu.base.exception.ProcessException;
import com.zzu.base.mapper.PermissionMapper;
import com.zzu.base.mapper.RoleMapper;
import com.zzu.base.mapper.RolePermissionMapper;
import com.zzu.base.mapper.UserRoleMapper;
import com.zzu.base.service.RoleService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
public class RoleServiceImpl extends ServiceImpl<RoleMapper, RoleDO> implements RoleService {
    @Resource
    private RoleMapper roleMapper;
    @Resource
    private PermissionMapper permissionMapper;
    @Resource
    private RolePermissionMapper rolePermissionMapper;
    @Resource
    private UserRoleMapper userRoleMapper;

    @Override
    public List<PermissionDO> getPermissionsByRoleId(Long roleId) {
        QueryWrapper<RolePermissionDO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("role_id", roleId);
        List<RolePermissionDO>rolePermissionDOS= rolePermissionMapper.selectList(queryWrapper);
        if(rolePermissionDOS==null ){
            throw new ProcessException(ResultStatusEnum.BAD_REQUEST,"参数错误");
        }
        List<PermissionDO> permissionDOS=new ArrayList<>();
        for(RolePermissionDO rolePermissionDO:rolePermissionDOS){
            permissionDOS.add(permissionMapper.selectById(rolePermissionDO.getPermissionId()));
        }
        return permissionDOS;
    }

    @Override
    public Boolean addRolePermission(Long roleId, Long permissionId) {
        RoleDO roleDO = roleMapper.selectById(roleId);
        PermissionDO permissionDO = permissionMapper.selectById(permissionId);
        if(roleDO == null || permissionDO == null){
            throw new ProcessException(ResultStatusEnum.BAD_REQUEST,"参数错误");
        }
        RolePermissionDO rolePermissionDO = new RolePermissionDO();
        rolePermissionDO.setRoleId(roleId);
        rolePermissionDO.setPermissionId(permissionId);
        return rolePermissionMapper.insert(rolePermissionDO)>0;
    }

    @Override
    public Boolean removeRolePermission(Long roleId, Long permissionId) {
        QueryWrapper<RolePermissionDO>queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("role_id", roleId);
        queryWrapper.eq("permission_id", permissionId);
        return rolePermissionMapper.delete(queryWrapper)>0;
    }

    @Override
    public Boolean update(RoleDO role) {
        QueryWrapper<RoleDO>queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("role_code", role.getRoleCode());
        RoleDO roleDO = roleMapper.selectOne(queryWrapper);
        if(roleDO!=null){
            throw new ProcessException(ResultStatusEnum.BAD_REQUEST,"已有重复角色代码");
        }
        return roleMapper.updateById(role)>0;
    }

    @Override
    public IPage<RoleDO> getRolePage(RolePageReqVO rolePageReqVO) {
        Page<RoleDO>page = new Page<>(rolePageReqVO.getPageNum(),rolePageReqVO.getPageSize());
        QueryWrapper<RoleDO> queryWrapper = new QueryWrapper<>();
        if (rolePageReqVO.getRoleName() != null) {
            queryWrapper.like("role_name", rolePageReqVO.getRoleName());
        }
        return roleMapper.selectPage(page,queryWrapper);
    }


    @Override
    @Transactional
    public Boolean removeRole(Long id) {
        QueryWrapper<RolePermissionDO>queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("role_id", id);
        rolePermissionMapper.delete(queryWrapper);
        QueryWrapper<UserRoleDO> queryWrapper2 = new QueryWrapper<>();
        queryWrapper2.eq("user_id", id);
        userRoleMapper.delete(queryWrapper2);
        return roleMapper.deleteById(id)>0;
    }
}
